<?php

namespace app\api\service;

use app\common\BaseHelper;

use app\common\model\ExpressTrace;
//use app\admin\model\Member as MemberMdl;
use app\admin\model\Membercoupon;
use app\admin\model\Order;
use app\admin\model\Orderitem;
use app\admin\model\MessageNotice;

use think\Db;
use think\Exception;

class Member
{

    /**
     * 登录后执行操作
     */
    public static function afterLogin($memberId){
        if (empty($memberId)) {
            return ['code' => 0, 'msg' => '参数错误'];
        }

        try {
            /*
            // 查看物流信息，是否有更新
            $orders = Order::where(['member_id' => $memberId, 'status' => 3])->field('id, order_id, express_no')->select();
            if ($orders) {
                $messLogisticsDB = db('message_logistics');
                foreach ($orders as $order) {
                    // 物流接口 查看物流信息
                    $expressTrace = ExpressTrace::where(['express_no' => $order['express_no']])->find();
                    if ($expressTrace) {
                        $traces = json_decode($expressTrace['traces'], true);
                        foreach ($traces as $trace) {
                            // 是否存在旧的物流消息
                            $logisticsMes = $messLogisticsDB->where(['member_id' => $memberId, 'order_id' => $order['id'], 'create_date' => $trace['AcceptTime']])->find();
                            if ($logisticsMes) {
                               continue;
                            }
                            // 添加物流消息 -- 只添加最新的物流消息
                            $logisticsData = [
                                //'create_date' => date('Y-m-d H:i:s'),
                                'create_date' => $trace['AcceptTime'],
                                'member_id' => $memberId,
                                //'order_id' => $order['id'],
                                'express_no' => $order['express_no'],
                                'title' => $trace['AcceptStation'],
                                'detail' => $trace['AcceptStation'],
                            ];
                            $logisticsMesRes = $messLogisticsDB->insert($logisticsData);
                        }
                    }
                }
            }
            */
        } catch (Exception $e) {
            trace($e->getMessage());
        }
    }
}